package com.example.pets.view.activity;


import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.pets.R;
import com.example.pets.model.entities.Post;
import com.example.pets.model.entities.dao.PostDao;
import com.example.pets.model.entities.database.AppDatabase;


import java.util.List;

public class PostDetailActivity extends AppCompatActivity {

    private TextView tvAuthorName, tvContent, tvLocationInfo, tvLikes, tvComments;
    private ImageView ivAuthorAvatar;
    private LinearLayout containerImageList;
    private AppDatabase database;
    private PostDao postDao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail);

        // 初始化视图
        tvAuthorName = findViewById(R.id.tv_author_name);
        tvContent = findViewById(R.id.tv_content);
        tvLocationInfo = findViewById(R.id.tv_location_info);
        tvLikes = findViewById(R.id.tv_likes);
        tvComments = findViewById(R.id.tv_comments);
        ivAuthorAvatar = findViewById(R.id.iv_author_avatar);
        containerImageList = findViewById(R.id.container_image_list);

        database = AppDatabase.getDatabase(this);
        postDao = database.postDao();

        int postId = getIntent().getIntExtra("postId", -1);
        if (postId != -1) {
            new LoadPostTask().execute(postId);
        }
    }

    private class LoadPostTask extends AsyncTask<Integer, Void, Post> {

        @Override
        protected Post doInBackground(Integer... integers) {
            return postDao.getPostByid(integers[0]);
        }

        @Override
        protected void onPostExecute(Post post) {
            if (post != null) {
                displayPostData(post);
            }
        }
    }

    private void displayPostData(Post post) {
        // 设置头像
        if (post.getAuthorurl() != null && !post.getAuthorurl().isEmpty()) {
            Glide.with(this)
                    .load(post.getAuthorurl())
                    .placeholder(R.mipmap.icon_tou)
                    .into(ivAuthorAvatar);
        }

        // 设置昵称
        tvAuthorName.setText(post.getAuthorname());

        // 设置内容
        tvContent.setText(post.getContent());

        // 设置位置信息
        String locationText = "";
        if (post.getAdress() != null && !post.getAdress().isEmpty()) {
            locationText += "\n地址: " + post.getAdress();
        }
        tvLocationInfo.setText(locationText);

        // 设置点赞和评论数量
        tvLikes.setText("点赞数: " + post.getLikesCount());
        tvComments.setText("评论数: " + post.getCommentsCount());

        // 设置图片列表
        List<String> imageUrls = post.getImageUrls();
        if (imageUrls != null && !imageUrls.isEmpty()) {
            for (String url : imageUrls) {
                ImageView imageView = new ImageView(this);

                // 设置LayoutParams，使用MATCH_PARENT或具体的dp值来适应不同设备
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT, // 假设定义了dimen资源文件中的image_width
                        LinearLayout.LayoutParams.WRAP_CONTENT
                );

                imageView.setLayoutParams(params);

                // 使用Glide加载图片，并设置圆角
                Glide.with(this)
                        .load(url)
                        .placeholder(R.mipmap.icon_nopng)
                        .into(imageView);

                // 添加阴影效果（通过设置背景）
                imageView.setBackgroundResource(R.drawable.shadow_image_background); // 需要定义相应的drawable资源

                // 在UI线程中添加ImageView到容器中
                runOnUiThread(() -> containerImageList.addView(imageView));
            }
        }
    }
}